<template>
    <el-container>
        <vHeadSpace />
        <el-container>
            <vAsideSpace />
            <el-container>
                <el-main class="main">
                    <div class="outcontainer_right">
                        <div class="block">
                            <el-date-picker
                                    v-model="chooseTime"
                                    type="datetimerange"
                                    :picker-options="pickerOptions"
                                    @change="selectchange()"
                                    format="yyyy-MM-dd"
                                    unlink-panels
                                    range-separator="-"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                                    :default-time="['00:00:00', '23:59:59']">
                            </el-date-picker>
                            <el-button type="primary" size="small" style="margin-left:5px" @click="exportClick()">信息报表导出</el-button>
                            <el-button type="primary" size="small" style="margin-left:5px" @click="exportClick2()">统计报表导出</el-button>
                        </div>
                        <div class="content">

                          <div id="bar2"></div>
                          <div id="bar"></div>
<!--                        <div class="table_info">-->
<!--                            <el-table-->
<!--                                    :data="tableData"-->
<!--                                    border-->
<!--                            >-->
<!--                                <el-table-column-->
<!--                                        prop="name"-->
<!--                                        label=""-->
<!--                                        min-width="10%">-->
<!--                                    <template slot-scope="scope">-->
<!--                                        {{ scope.row.name }}-->
<!--                                    </template>-->
<!--                                </el-table-column>-->
<!--                                <el-table-column-->
<!--                                        prop="weifenlei"-->
<!--                                        label="未分类"-->
<!--                                        min-width="10%">-->
<!--                                    <template slot-scope="scope">-->
<!--                                        {{ scope.row.weifenlei }}-->
<!--                                    </template>-->
<!--                                </el-table-column>-->
<!--                                <el-table-column-->
<!--                                        prop="xinxi"-->
<!--                                        label="信息"-->
<!--                                        min-width="10%">-->
<!--                                    <template slot-scope="scope">-->
<!--                                        {{ scope.row.xinxi }}-->
<!--                                    </template>-->
<!--                                </el-table-column>-->
<!--                                <el-table-column-->
<!--                                        prop="jinggao"-->
<!--                                        label="警告"-->
<!--                                        min-width="10%">-->
<!--                                    <template slot-scope="scope">-->
<!--                                        {{ scope.row.jinggao }}-->
<!--                                    </template>-->
<!--                                </el-table-column>-->
<!--                                <el-table-column-->
<!--                                        prop="yibanyanzhong"-->
<!--                                        label="一般严重"-->
<!--                                        min-width="10%">-->
<!--                                    <template slot-scope="scope">-->
<!--                                        {{ scope.row.yibanyanzhong }}-->
<!--                                    </template>-->
<!--                                </el-table-column>-->
<!--                                <el-table-column-->
<!--                                        prop="yanzhong"-->
<!--                                        label="严重"-->
<!--                                        min-width="10%">-->
<!--                                    <template slot-scope="scope">-->
<!--                                        {{ scope.row.yanzhong }}-->
<!--                                    </template>-->
<!--                                </el-table-column>-->
<!--                                <el-table-column-->
<!--                                        prop="zainan"-->
<!--                                        label="灾难"-->
<!--                                        min-width="10%">-->
<!--                                    <template slot-scope="scope">-->
<!--                                        {{ scope.row.zainan }}-->
<!--                                    </template>-->
<!--                                </el-table-column>-->
<!--                            </el-table>-->
<!--                        </div>-->
                        </div>
                    </div>


                </el-main>
                <vFootSpace />
            </el-container>
        </el-container>
    </el-container>
</template>

<script>
    import axios from 'axios'
    export default {
        name: 'PerformanceExcel',
        data() {
            return {
                hostType: "0",
                name: '',
                chooseTime:[new Date().getTime()-3600*1000*24*7,new Date().getTime()],
                tableData:[],
                data1: [],
                data2: [],
                data3: [],
                top10_x:[],
                top10_y:[],
                echarts: '',
                pickerOptions: {
                    disabledDate(time) {
                        const start = new Date(new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1);
                        return time.getTime() >= start;
                    },
                    shortcuts: [{
                        text: '最近一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一年',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 365);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
            }
        },
        methods:{
            selectchange(){
                this.InitParams();
                this.getTop10Data();
                this.getEchartsData();
            },
            exportClick() {
                this.$confirm('此操作将导出该条件下的所有数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios({
                        method: "post",
                        url: '/report/export_alert_excel.action',
                        data: {
                            startDate: (new Date(this.chooseTime[0])).getTime(),
                            endDate: (new Date(this.chooseTime[1])).getTime()
                        },
                        crossOrigin: true,
                        crossDomain: true,
                        responseType: 'blob',
                    }).then((response) => {
                        const link = document.createElement('a')
                        let blob = new Blob([response.data], {type: 'application/vnd.ms-excel'})
                        link.style.display = 'none'
                        link.href = URL.createObjectURL(blob)
                        let fileName = "告警信息报表"
                        link.download = fileName
                        document.body.appendChild(link);
                        link.click();
                        document.body.removeChild(link);
                    }).catch((error) => {
                        console.log(error);
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消导出'
                    });
                });
            },
            getTop10Data(){
                this.top10_x = []
                this.top10_y = []
                console.log()
                this.$axios({
                    method:"post",
                    url:'/report/show_alert_top10.action',
                    data:{
                        startDate: (new Date(this.chooseTime[0])).getTime(),
                        endDate: (new Date(this.chooseTime[1])).getTime(),
                    }
                }).then((res)=>{
                    for (let i = 0; i < res.data.content.result.length; i++) {
                        this.top10_x[i] = res.data.content.result[i].host
                        this.top10_y[i] = res.data.content.result[i].count
                    }
                  this.drawTop10Echarts()
                })
            },

            drawTop10Echarts(){
                let myChart = this.$echarts.init(document.getElementById("bar2"));

                let option = {
                    title:{
                        text:'历史告警统计Top10',
                        left: '40%',
                        top: '2%'
                    },
                    grid: {

                        left: '5%',
                        right: '10%',
                        bottom: '10%',
                        containLabel: true
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                        }
                    },
                    xAxis: {
                        axisLabel: {
                            interval:0,
                            rotate:40
                        },
                        type: 'category',
                        data: this.top10_x
                    },
                    yAxis: {
                        type: 'value',
                        name: '(个)'
                    },
                    series: [
                        {
                            data: this.top10_y,
                            type: 'bar',
                            // barGap: '50%',
                            // barCategoryGap: '30%',
                            // showBackground: true,
                            // backgroundStyle: {
                            //     color: 'rgba(180, 180, 180, 0.2)'
                            // }
                        }
                    ]
                };
                option && myChart.setOption(option);
                return myChart;
            },
            InitParams(){
                this.top10_x = []
                this.top10_y = []
                this.data1 = []
                this.data2 = []
                this.data3 = []
                this.tableData = []
            },
            getEchartsData(){
                this.$axios({
                    method: "post",
                    url: '/report/getChartsData.action',
                    data: {
                        startDate: (new Date(this.chooseTime[0])).getTime(),
                        endDate: (new Date(this.chooseTime[1])).getTime()
                    }
                }).then((res) => {
                    this.data1 = res.data.content.result[0]
                    this.data2 = res.data.content.result[1]
                    this.data3 = res.data.content.result[2]
                    this.tableData = res.data.content.result[3]
                    this.drawEcharts();
                })
            },
            drawEcharts() {
                let myChart = this.$echarts.init(document.getElementById("bar"));

                let option = {
                    title:{
                      text:'告警统计',
                      left: '10%'
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                        }
                    },
                    legend: {
                        data: [ '未处理警告', '历史警告', '总和'],
                        right: '10%'
                    },
                    grid: {
                        left: '5%',
                        right: '10%',
                        bottom: '10%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: ['未分类', '信息类', '警告', '一般严重', '严重', '灾难']
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            name: '(个)'
                        }
                    ],
                    series: [
                        {
                            name: '未处理警告',
                            type: 'bar',
                            stack: '广告',
                            emphasis: {
                                focus: 'series'
                            },
                            data: this.data1
                        },
                        {
                            name: '历史警告',
                            type: 'bar',
                            stack: '广告',
                            emphasis: {
                                focus: 'series'
                            },
                            data: this.data2
                        },
                        {
                            name: '总和',
                            type: 'line',
                            data: this.data3
                        }
                    ]
                };

                option && myChart.setOption(option);
                return myChart;
            },
            exportClick2() {
                this.echarts = this.drawEcharts();
                // console.log(this.echarts.getDataURL())
                this.$confirm('此操作将导出该条件下的所有数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios({
                        method: "post",
                        url: '/report/export_alert_statistics_excel.action',
                        data: {
                            // startDate: '2021-08-11 08:43:28',
                            // endDate: '2021-09-10 08:43:28',
                            startDate: (new Date(this.chooseTime[0])).getTime(),
                            endDate: (new Date(this.chooseTime[1])).getTime(),
                            imgsURl: this.echarts.getDataURL()
                        },
                        crossOrigin: true,
                        crossDomain: true,
                        responseType: 'blob',
                    }).then((response) => {
                        const link = document.createElement('a')
                        let blob = new Blob([response.data], {type: 'application/vnd.ms-excel'})
                        link.style.display = 'none'
                        link.href = URL.createObjectURL(blob)
                        let fileName = "告警统计报表"
                        link.download = fileName
                        document.body.appendChild(link);
                        link.click();
                        document.body.removeChild(link);
                    }).catch((error) => {
                        console.log(error);
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消导出'
                    });
                });
            },
        },
        mounted() {
            this.getTop10Data();
            this.drawEcharts();
            this.getEchartsData();
            // this.exportClick2();
        },
        watch:{
            chooseTime(newdata) {
                // console.log(newdata)
                this.getEchartsData();
            }
        }
    }
</script>
<style scoped>
    .main{
        height: 88vh;
        width: 100%;
        margin-top: 60px;
        padding:10px;
    }
    .outcontainer{
        display: flex;
        margin-top: 60px;
        height: 92%;
        width: 100%;
    }
    .outcontainer_right{
        height: 100%;
    }
    .content{
        margin-top: 0;
        width: 100%;
        height: 97%;
    }
    #hostsetting {
        height: 100%;
        width: 100%;
        background-color: #F0F2F5;
        position: absolute;
    }
    .itemInfo-middle-type div{
        display: inline;
        padding-left: 5px;
        padding-right: 5px;
        height: 30px;
        line-height: 50px;
        color: #d9d9d9;
        border-top: 1px solid #d9d9d9;
        border-left: 1px solid #d9d9d9;
        border-bottom: 1px solid #d9d9d9;
    }
    #bar{
        margin: 0 auto;
        width: 800px;
        height: 600px;
    }
    #bar2{
        width: 800px;
        height: 600px;
        margin: 0 auto;
    }

</style>
